package com.bw.goods_module.detail;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.alibaba.android.arouter.launcher.ARouter;
import com.bw.common.mvvm.viewmodel.BaseViewModel;
import com.bw.common.providerservice.CartApi;
import com.bw.common.providerservice.CheckLoginApi;
import com.bw.common.utils.ToastShowUtils;
import com.bw.goods_module.home.GoodsEntity;
import com.bw.goods_module.home.HomeEntity;
import com.bw.goods_module.home.adapter.GoodsAdapter;
import com.bw.network_sdk.BaseEntity;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class GoodsDetailViewModel extends
        BaseViewModel<GoodsDetailRespository> implements OnItemClickListener {

    public long goodsId;
    public long categoryId;
    public ObservableField<GoodsEntity>detailGoods = new ObservableField<>();
    public ObservableField<List<GoodsEntity>>goodsList = new ObservableField<>();
    private CheckLoginApi checkLoginApi;
    private CartApi cartApi;

    public void backMenu(){
        startPage("/app/frameactivity");
    }

    public void share(){

    }

    public void carList(){

    }

    public void addCar(){
        Map<String,Object>map=new HashMap<>();
        map.put("goodsCount",1);
        map.put("goodsDesc",detailGoods.get().goods_desc);
        map.put("goodsIcon",detailGoods.get().goods_default_icon);
        map.put("goodsId",detailGoods.get().id);
        map.put("goodsPrice",detailGoods.get().goods_default_price);
        map.put("goodsSku",detailGoods.get().goods_default_sku);
        map.put("time",System.currentTimeMillis());
        map.put("userId",checkLoginApi.getUserId());
        cartApi.insertCart(map)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onResult(BaseEntity entity) {
        if (entity instanceof GoodsDetailEntity){
            //商品详情返回
            GoodsDetailEntity goodsDetailEntity = (GoodsDetailEntity) entity;
            detailGoods.set(goodsDetailEntity.data);
        }else if (entity instanceof HomeEntity){
            //推荐商品返回
            HomeEntity homeEntity = (HomeEntity) entity;
            goodsList.set(homeEntity.data);
        }else {
            //添加返回
            ToastShowUtils.showShort(entity.message);
        }
    }

    @Override
    protected void initData() {
        checkLoginApi = (CheckLoginApi) ARouter.getInstance()
                .build("/user/checklogin").navigation();
        cartApi = (CartApi) ARouter.getInstance()
                .build("/car/cartapi").navigation();
        //请求商品详情参数
        Map<String,Object>detailMap = new HashMap<>();
        detailMap.put("goodsId",goodsId);
        //请求推荐商品参数
        Map<String,Object>listMap = new HashMap<>();
        listMap.put("categoryId",categoryId);
        listMap.put("pageNo",1);
        respository.request(detailMap,listMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(this);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        GoodsAdapter goodsAdapter = (GoodsAdapter) adapter;
        Bundle bundle = new Bundle();
        bundle.putLong("goodsid",goodsAdapter.getData().get(position).id);
        bundle.putLong("categoryid",categoryId);
        startBundPage("/home/detailactivity",bundle);
    }
}
